package com.huang.gulimail.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.huang.gulimail.product.entity.AttrEntity;
import com.huang.gulimail.product.service.AttrAttrgroupRelationService;
import com.huang.gulimail.product.service.AttrService;
import com.huang.gulimail.product.service.CategoryService;
import com.huang.gulimail.product.vo.AttrGroupRelationVo;
import com.huang.gulimail.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.huang.gulimail.product.entity.AttrGroupEntity;
import com.huang.gulimail.product.service.AttrGroupService;
import com.huang.common.utils.PageUtils;
import com.huang.common.utils.R;



/**
 * 属性分组
 *
 * @author huang
 * @email huang@gmail.com
 * @date 2021-10-08 10:28:19
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId){
        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
		Long catelogId=attrGroup.getCatelogId();
		Long []path=categoryService.findCatelogPath(catelogId);
		attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 分组关联属性查询
     * @return
     */
    @RequestMapping("/{attrgroupId}/attr/relation")
    public R list(@PathVariable("attrgroupId")Long attrgroupId){
        List<AttrEntity>entities=attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",entities);
    }

    /**
     * 关联分组属性删除
     * @return
     */
    @RequestMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos){
        attrService.deleteRelation(vos);
        return R.ok();
    }

    /**
     * 未进行分组的属性
     * @param attrgroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R noList(@RequestParam Map<String, Object> params,@PathVariable("attrgroupId")Long attrgroupId){
        PageUtils page=attrService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("page",page);
    }
    /**
     * 关联分组属性
     * @return
     */
    @RequestMapping("/attr/relation")
    public R relation(@RequestBody AttrGroupRelationVo[] vos){
        attrAttrgroupRelationService.relation(vos);
        return R.ok();
    }

    @RequestMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId")Long catelogId){
        //查询所有该分类下的属性组
        List<AttrGroupWithAttrsVo>vos=attrGroupService.getAttrGroupWithAttrsByCatlogId(catelogId);
        return R.ok().put("data",vos);
        //查询属性组下的所有属性
    }
}
